<?php
/**
 * Created by Alan.
 * User: Alan
 * Date: 2018\10\7 0007
 * Time: 6:05
 */

namespace app\common\model;


use think\Model;

class Category extends Model
{
    protected $autoWriteTimestamp = true;

    public function addOrUpdate($data)
    {
        $data['status'] = 1;
        if($data['id']){
            $where = ['id' => $data['id']];
        }
        else
        {
            $where = [];
        }
        return $this->save($data,$where);
    }

    public static function getNormalFirstCategory()
    {
        $categories = self::where(['status' => 1,'parent_id' =>0])
            ->order(['id' => 'desc'])
            ->select();
        return $categories;
    }

    public static function getFirstCategorys($parendId = 0)
    {
        $data = [
            'parent_id' => $parendId,
            'status' => ['neq',-1]
        ];

        $order = [
            'listorder' => 'desc',
            'id' => 'desc'
        ];

        $result = self::where($data)
            ->order($order)
            ->paginate();
        return $result;
    }

    public static  function getNomalCategoryByParentId($parendId = 0)
    {
        $data = [
            'parent_id' => $parendId,
            'status' => '1'
        ];

        $order = [
            'id' => 'desc'
        ];

        return self::where($data)
            ->order($order)
           ->select();
    }

    //修改分类状态
    public function changeStatus($data)
    {
        $status = intval($data['status']);
        $id = intval($data['id']);
        return $this->save(['status' =>$status],['id'=>$id]);
    }

    //改变排序
    public function changeListOrder($data)
    {
        $id = intval($data['id']);
        $listorder = intval($data['listorder']);
        return $this->save(['listorder' => $listorder],['id' => $id]);
    }
}